import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPool{
    // 任务队列 ;
      private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(20);
      // submit方法 添加任务
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    // 给一个参数,作为线程池最大线程数量 , 相当于 ThreadFixedThread
    // 创建线程来执行 队列里的方法
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                try {
                    Runnable runnable = queue.take();
                       runnable.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();;
                }
            });
            t.start();
        }

    }
}

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(4);
        for (int i = 1; i < 500; i++) {
            int id = i ;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(id);
                }
            });
        }

    }
}
